'use strict';

const baseController = require('../../utils/base')

class LoginController extends baseController {
    async index() {
        await this.ctx.render(`/${this.config.pathname}/login`)
    }
    async captcha() {
            let captcha = await this.service.admin.utlis.captcha()
            console.log(captcha.text);
            this.ctx.session.code = captcha.text
            this.ctx.response.type = 'image/svg+xml'
            this.ctx.body = captcha.data
        }
        // 登录
    async dologin() {
            let { verify, username, password } = this.ctx.request.body;
            password = await this.service.admin.utlis.md5(password)
            console.log(this.ctx.session.code, '---------', verify, password);
            if (verify == this.ctx.session.code) {
                let resuls = await this.app.mysql.query('select * from admin where username = ? and password = ?', [username, password])
                console.log(resuls, "----");
                if (resuls.length > 0) {
                    this.ctx.session.userinfo = resuls[0]
                    await this.success('登录成功', `/${this.config.pathname}`)
                } else {
                    await this.error('用户名或密码错误', `/${this.config.pathname}/login`)
                }
            } else {
                await this.error('验证码错误', `/${this.config.pathname}/login`)
            }
        }
        //  退出登录
    async logut() {
        console.log(await this.service.admin.utlis.md5(this.ctx.session.userinfo));
        this.ctx.session.userinfo = null
        await this.success('退出登录成功', `/${this.config.pathname}/login`)
    }
}

module.exports = LoginController;